package com.knight.interview.yanlian;

/**
 * 比较一个字符串的从前到后和从后到前的顺序是否一样
 * 如：abcba一致
 * @author Administrator
 *
 */
public class YL_Quest2 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String str = "abcba";
		try {
			System.out.println(checkStringOrder(str));
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	private static boolean checkStringOrder(String str)throws Exception{
		if(null == str || "".equals(str)){
			throw new Exception("要比较的字符串为空");
		}
		int length = str.length(), left = 0, right = 0;
		StringBuffer sbf = new StringBuffer(str);
		if(length == 1){
			return true;
		}else{
			switch(length%2){
			case 0 :
				left = length/2 - 1;
				right = left + 1;
				break;
			case 1 :
				left = length/2 - 1;
				right = left + 2;
				break;
			}
		}
		String leftString = sbf.substring(0, length/2);
		String rightString = sbf.substring(right);
		if(new StringBuffer(leftString).reverse().toString().equals(rightString)){
			return true;
		}else{
			return false;
		}
	}
}
